﻿using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using BitPortal.Models;
using BitPortal.Utils;
using System.Globalization;
using System.Resources;
using System.Diagnostics;
using System.IO;
using System.Collections;

namespace BitPortal.Business
{
    public static partial class LanguageBLL
    {
        public static IQueryable<Language> GetAll()
        {
            return (new ExtendedBitPortalEntities()).Languages.Where(ent => ent.IsDeleted == false);
        }

        public static Language GetByLanguageCode(string languageCode)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            return bitEnt.Languages.SingleOrDefault(ent => ent.LanguageCode == languageCode);
        }

        public static Language GetDefault()
        {
            return GetByLanguageCode(PortalConfigBLL.GetValueByName("DefaultLanguage").ToString());
        }

        public static int Save(this Language lang)
        {
            if (lang.LanguageCode.Length < 2 || lang.LanguageCode.Length > 10)
            {
                throw new Exception("Mã ngôn ngữ không hợp lệ!");
            }
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            Language l = bitEnt.Languages.SingleOrDefault(ent => ent.LanguageCode == lang.LanguageCode);
            if (l == null)
            {
                if (CultureInfo.GetCultureInfo(lang.LanguageCode) == null)
                {
                    throw new Exception("Mã ngôn ngữ không hợp lệ!");
                }
                bitEnt.Languages.AddObject(lang);
                CopyDefaultResXResourceData(lang.LanguageCode);
                return bitEnt.SaveChanges();
            }
            l.Name = lang.Name;
            l.IsEnabled = lang.IsEnabled;
            l.FlagImage = lang.FlagImage;
            l.IsDeleted = false;
            return bitEnt.SaveChanges();
        }

        public static int DeletePermantly(string languageCode)
        {
            if (languageCode.Equals(PortalConfigBLL.GetValueByName("DefaultLanguage")))
            {
                throw new Exception("Không thể xóa ngôn ngữ mặc định!");
            }
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            Language lang = bitEnt.Languages.SingleOrDefault(ent => ent.LanguageCode == languageCode);
            if (lang != null)
            {
                bitEnt.Languages.DeleteObject(lang);
            }
            return bitEnt.SaveChanges();
        }

        public static int Delete(string languageCode)
        {
            if (languageCode.Equals(PortalConfigBLL.GetValueByName("DefaultLanguage")))
            {
                throw new Exception("Không thể xóa ngôn ngữ mặc định!");
            }
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            Language lang = bitEnt.Languages.SingleOrDefault(ent => ent.LanguageCode == languageCode);
            if (lang != null)
            {
                lang.IsDeleted = true;
            }
            return bitEnt.SaveChanges();
        }

        public static string GetResourceFileByLanguageCode(string languageCode = null)
        {
            if (languageCode != null)
            {
                return HttpContext.Current.Server.MapPath("~/App_GlobalResources/BitResources." + languageCode + ".resx");
            }
            else return HttpContext.Current.Server.MapPath("~/App_GlobalResources/BitResources.resx");
        }

        public static Dictionary<string, string> ReadResXFromResourceFile(string filePath)
        {
            Dictionary<string, string> resources = new Dictionary<string, string>();
            System.Resources.ResXResourceReader reader = new ResXResourceReader(filePath);
            foreach (DictionaryEntry entry in reader)
            {
                resources.Add(entry.Key.ToString(), entry.Value.ToString());
            }
            reader.Close();
            return resources;
        }

        public static void WriteResXResourceFile(string filename, Dictionary<string, string> resources)
        {
            ResXResourceWriter writer = new ResXResourceWriter(filename);
            foreach (string key in resources.Keys)
            {
                writer.AddResource(key, resources[key]);
            }
            writer.Close();
        }

        public static void CopyDefaultResXResourceData(string languageCode)
        {
            Dictionary<string, string> d = ReadResXFromResourceFile(GetResourceFileByLanguageCode());
            WriteResXResourceFile(GetResourceFileByLanguageCode(languageCode), d);
        }
    }
}